package cn.sypt.yuhao.model;

import java.sql.*;
import java.util.ArrayList;

import cn.sypt.yuhao.domain.User;

import com.mysql.jdbc.Connection;

public class UserBeanBO {
	
	Connection con=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	
	boolean a;
	
	//��֤��¼����
	public boolean checkUser(String u,String p)
	{
		a=false;
	
		try {
			
			con=(Connection) new DBcon().getConnection();
			
			String sql="select upass from user where uname='"+u+"'";
			ps=con.prepareStatement(sql);
			
			rs=ps.executeQuery();
			
			if(rs.next())
			{
				String upass=rs.getString(1);
				if(upass.equals(p))
				{
				System.out.println("uname="+u+",upass="+upass);
				a=true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		return a;
	}
	
	
	//ע���û�����
	public boolean zhuce(String uid,String uname,String upass,String uemail,String uaddress,String ubirth,String uphone)
	{
		int num=0;
	
		a=false;
		try {
			
			con=(Connection) new DBcon().getConnection();
			String sql="insert into user values('"+uid+"','"+uname+"','"+upass+"','"+uemail+"','"+uaddress+"','"+ubirth+"','"+uphone+"');";
			ps=con.prepareStatement(sql);
			
			num=ps.executeUpdate();
			if(num==1)
			{
				a=true;
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return a;
	}
	
	//�õ�userBean	
	public User getUser(String uname)
	{
		User ub=new User();
		
		try {
			
			con=(Connection) new DBcon().getConnection();
			
			String sql="select * from user where uname='"+uname+"'";
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			
			if(rs.next())
			{
				ub.setUid(rs.getString(1));
				ub.setUname(rs.getString(2));
				ub.setUpass(rs.getString(3));
				ub.setUemail(rs.getString(4));
				ub.setUaddress(rs.getString(5));
				ub.setUbirth(rs.getString(6));
				ub.setUphone(rs.getString(7));
				ub.setUimage(rs.getString(8));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return ub;
		
	}
	
	//��ҳ�������ܺ���
	public ArrayList shouYeSearch(String s)
	{
		ArrayList al=new ArrayList();
		try {
			
			con=(Connection) new DBcon().getConnection();
			
			String sql="select sptpurl from video where spname like '%"+s+"%'";
			
			ps=con.prepareStatement(sql);
			
			rs=ps.executeQuery();
		    
		   while(rs.next())
		    {
		    	String a=rs.getString(1);
		    	al.add(a);
		    	
		    	
		    }
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return al;
	}
	
	
	//�رս����
	public void close()
	{
		try {
			
			if(rs!=null)
			{
				rs.close();
				rs=null;
			}
			if(ps!=null)
			{
				ps.close();
				ps=null;
			}
			if(con!=null)
			{
				con.close();
				con=null;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
